package com.hzjy.api.controller;

import com.hzjy.api.beans.HzjyUserBill;
import com.hzjy.api.service.BqTokenService;
import com.hzjy.api.service.impl.HzjyUserBillServiceImpl;
import com.hzjy.api.vo.BqLoginUser;
import com.hzjy.common.constant.HttpStatus;
import com.hzjy.common.core.controller.BaseController;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.core.page.TableDataInfo;
import com.hzjy.common.utils.PageData;
import com.hzjy.common.utils.ServletUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户账单Controller
 *
 * @author hzjy
 * @date 2020-08-26
 */
@RestController
@RequestMapping("/api/bill")
public class HzjyUserBillController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(HzjyUserBillController.class);
    @Autowired
    private HzjyUserBillServiceImpl hzjyUserBillService;
    @Autowired
    private BqTokenService bqTokenService;

    /**
     * 查询用户账单列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody HzjyUserBill hzjyUserBill) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("未登录");
            return rspData;
        }
        hzjyUserBill.setUserId(loginUser.getBqUser().getUserId().toString());

        startPage();
        List<HzjyUserBill> list = hzjyUserBillService.selectHzjyUserBillList(hzjyUserBill);
        return getDataTable(list);
    }

//    /**
//     * 查询用户账单列表
//     */
//    @ApiOperation("查询用户账单列表")
//    @PostMapping("/list")
//    public AjaxResult list(@RequestBody PageData pageDomain) {
//        log.info("【查询用户账单列表】,args:" + JSONUtil.object2json(pageDomain));
//        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
//        if (loginUser == null) {
//            return AjaxResult.error(401, "未登录");
//        }
//        Integer pageNum = Integer.parseInt(pageDomain.get("pageNum").toString());
//        Integer pageSize = Integer.parseInt(pageDomain.get("pageSize").toString());
//        TableDataInfo dataInfo = hzjyUserBillService.selectBillListForPage(pageNum, pageSize, pageDomain);
//        log.info("【查询用户账单列表】" + JSONUtil.object2json(dataInfo));
//        return AjaxResult.success(200, "查询成功", dataInfo);
//    }

    /**
     * 查询用户账单列表
     */
    @PostMapping("/wxList")
    public AjaxResult wxList(@RequestBody PageData pageData) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401,"未登录");
        }
        pageData.put("userId",loginUser.getBqUser().getUserId().toString());
        //hzjyUserBill.setUserId(loginUser.getBqUser().getUserId().toString());
        if(pageData.isEmpty()){
            pageData.put("currentPage",0);
            pageData.put("size",10);
        }else{
            pageData.put("currentPage",(Integer.parseInt(pageData.get("currentPage").toString())-1) * Integer.parseInt(pageData.get("size").toString()));
        }
        List<PageData> list = hzjyUserBillService.queryHzjyUserBillList(pageData);
        PageData count = hzjyUserBillService.getUserBillNum(pageData);
        Map map = new HashMap();
        map.put("userBillList", list);
        map.put("totalResult", count.get("totalResult"));
        return AjaxResult.success(200,"查询成功",map);
    }

}
